package com.boyadegroot.divelog.ui.diver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.models.Diver;

public class DiverTabs extends ActionBarActivity {

    private static final String TAG = DiverTabs.class.getName();

    private static final String STATE_CURRENT_SECTION = "currentSection";
    
    private static final int EDIT_DIVER_REQUEST = 1;

    private Diver mDiver;
    
    private ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Log.v(TAG, "onCreate(Bundle savedInstanceState)");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager_diver_tabs);
        Context context = getApplicationContext();
        mDiver = getIntent().getParcelableExtra("diver");
        // setTitle(mDiver.getUsername());
        FragmentManager fm = getSupportFragmentManager();
        DiverAdapter adapter = new DiverAdapter(fm, mDiver, context);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(adapter);
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }
            @Override
            public void onPageSelected(int i) {
                invalidateOptionsMenu();
            }
            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Log.v(TAG, "onCreateOptionsMenu(Menu menu)");
        getMenuInflater().inflate(R.menu.menu_diverpager, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // Log.v(TAG, "onPrepareOptionsMenu(Menu menu)");
        int page = mPager.getCurrentItem();
        // Log.v(TAG, "Current page = " + page);
        menu.findItem(R.id.menuitem_newdive).setVisible(
                page == DiverAdapter.PAGE_DIVES
        );
        menu.findItem(R.id.menuitem_importsde).setVisible(
                true
        );
        menu.findItem(R.id.menuitem_newcert).setVisible(
                page == DiverAdapter.PAGE_CERTS
        );
        menu.findItem(R.id.menuitem_editdiver).setVisible(
                true
        );
        menu.findItem(R.id.menuitem_deletediver).setVisible(
                true
        );
        return super.onPrepareOptionsMenu(menu);
    }


    public void onEditDiverClick(MenuItem item) {
        Intent intent = new Intent(this, DiverEdit.class);
        intent.putExtra(DiverEdit.IN_DIVER, mDiver);
        // Log.d(TAG, "Diver to edit: " + mDiver);
        this.startActivityForResult(intent, EDIT_DIVER_REQUEST);
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Log.v(TAG, "onActivityResult(int requestCode, int resultCode, Intent data)");
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EDIT_DIVER_REQUEST) {
            // If the result was OK, a the Diver was edited
            if (resultCode == RESULT_OK) {
                // Log.d(TAG, "resultCode == RESULT_OK");
            }
        }
    }

    public void onDeleteDiverClick(MenuItem item) {
        // TODO: When the user clicks on Delete diver, we show a confirmation dialog
    }

    public void onNewDiveClick(MenuItem item) {
        // TODO: Add dive activity
    }

    public void onImportSdeClick(MenuItem item) {
        // Log.v(TAG, "onImportSdeClick(MenuItem item)");
        Intent importSde = new Intent(this, DiverImportContainer.class);
        importSde.putExtra("diver", mDiver);
        startActivity(importSde);
    }

    public void onNewCertClick(MenuItem item) {
        // TODO: Add certification activity
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        // Log.v(TAG, "onSaveInstanceState(Bundle outState)");
        outState.putInt(STATE_CURRENT_SECTION, mPager.getCurrentItem());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        // Log.v(TAG, "onRestoreInstanceState(Bundle savedInstanceState)");
        super.onRestoreInstanceState(savedInstanceState);
        int currentItem = savedInstanceState.getInt(STATE_CURRENT_SECTION);
        mPager.setCurrentItem(currentItem, false);
    }

}
